package com.google.gwt.it2tool.client.widget;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.it2tool.client.Factory;
import com.google.gwt.it2tool.client.widget.ProductLabel.IProductClickedHandler;
import com.google.gwt.user.client.ui.Label;

public class FactoryLabel extends Label {
	
	private ArrayList<IFactoryClickedHandler> factoryClickHandlers = new ArrayList<IFactoryClickedHandler>();
	
	private final Factory factory;

	public FactoryLabel(Factory fac){
		super(fac.getName());
		
		this.factory=fac;
		
		addStyleName("clickableLabel");
		addStyleName("factoryName");
		
		addClickHandler(new ClickHandler() {			
			public void onClick(ClickEvent event) {
				triggerOnProductClicked(factory, event);
			}
		});		
	}	

	public void addFactoryClickedHandler(IFactoryClickedHandler handler){
		factoryClickHandlers.add(handler);
	}
	
	public void removeFactoryClickedHandler(IFactoryClickedHandler handler){
		if(handler!=null && factoryClickHandlers.contains(handler)){
			factoryClickHandlers.remove(handler);
		}
	}
	
	public void triggerOnProductClicked(Factory factory,ClickEvent event){
		for(IFactoryClickedHandler h: factoryClickHandlers){
			h.onFactoryClicked(factory, event);
		}
	}
	
	public interface IFactoryClickedHandler{
		public void onFactoryClicked(Factory factory, ClickEvent event);
	}

}
